#include "VisualizationElement.h"


namespace OpenNano
{

    REGISTER_OBJECT_FACTORY ( VisualizationElement, VISUALIZATION_TAG );


    VisualizationElement::VisualizationElement(TiXmlElement * element) :
                                ConfigElement(element), logger(log4cplus::Logger::getInstance( VISUALIZATION_TAG )),
                                _fullscreen(false), _screen_width(1024), _screen_height(768), _scene_filename(), _resolution_processed(false),
                                _stereo(NO_STEREO), _visAPI(OPENGL_VALUE)
                                {
                                }


    VisualizationElement::~VisualizationElement()
    {
    }


    void VisualizationElement::getResolution(int &width, int &height)
    {
        width   = _screen_width;
        height  = _screen_height;
    }

    Stereo_Type VisualizationElement::getStereoChoice()
    {
        return _stereo;
    }

    bool VisualizationElement::isFullscreen()
    {
        return _fullscreen;
    }


    void VisualizationElement::init()
    {
        LOG4CPLUS_INFO(logger, "Visualization element: initialized");
    }

    void VisualizationElement::attributeFound(TiXmlAttribute * attribute)
    {
        if (    attribute->NameTStr().compare(STEREO_ATTRIBUTE) == 0 )
        {
            if ( attribute->ValueStr().compare(ACTIVE_STEREO_VALUE) == 0 )
            {
                _stereo=ACTIVE;
            }
            else if ( attribute->ValueStr().compare(DUAL_STEREO_VALUE) == 0 )
            {
                _stereo=DUAL;
            }
            else  //in any other case the safest option is...
            {
                _stereo=NO_STEREO; //no stereo!
            }
        }
        else if ( attribute->NameTStr().compare(FULLSCREEN_ATTRIBUTE) == 0 )
        {
            if ( attribute->ValueStr().compare(TRUE_VALUE) == 0 )
            {
                _fullscreen=true;
            }
            else  //in any other case the safest option is...
            {
                _fullscreen=false; //no fullscreen!
            }
        }
        else if ( attribute->NameTStr().compare(SCENE_ATTRIBUTE) == 0 )
        {
            _scene_filename = attribute->ValueStr();
        }
        else if ( attribute->NameTStr().compare(VIS_API_ATTRIBUTE) == 0 )
        {
            _visAPI = attribute->ValueStr();
            //no check
        }
        else
        {
            //attribute not recognized
            LOG4CPLUS_WARN(logger, "Attribute not recognized:");
            LOG4CPLUS_WARN(logger, attribute->NameTStr());

            unknownAttributeFound(attribute);
        }
    }


    void VisualizationElement::childElementFound( TiXmlElement * childElement )
    {
        if ( childElement->ValueStr().compare(RESOLUTION_TAG) == 0 )
        {
            if (!_resolution_processed)
            {
                TiXmlAttribute * attrib = childElement->FirstAttribute();
                int _h = 0;
                int _w = 0;
                bool _height_proc = false;
                bool _width_proc = false;
                while (attrib)
                {
                    if ( attrib->NameTStr().compare(RES_WIDTH_ATTRIBUTE) == 0 && !_width_proc &&
                            attrib->QueryIntValue(&_w)==TIXML_SUCCESS )
                    {
                       // _screen_width=attrib->IntValue();
                        _width_proc=true;
                    }
                    else if ( attrib->NameTStr().compare(RES_HEIGHT_ATTRIBUTE) == 0 && !_height_proc &&
                            attrib->QueryIntValue(&_h)==TIXML_SUCCESS )
                    {
                       // _screen_height=attrib->IntValue();
                        _height_proc=true;
                    }
                    else  //in any other case the safest option is...
                    {
                       LOG4CPLUS_WARN(logger, "Attribute invalid");
                       _resolution_processed=false; //dummy instruction
                    }
                    attrib=attrib->Next();
                }
                if (_width_proc && _height_proc)
                {
                    _screen_height=_h;
                    _screen_width=_w;
                    _resolution_processed=true;
                }
            }
            else
            {
                LOG4CPLUS_WARN(logger, "Resolution already specified, element ignored");
                //handle the case of multiple resolution inputs
                //or at least, warn against tag duplication!
                //anyway, i think it's clear that the only resolution tag processed is the first found
            }
        }
        else
        {

           //Element not recognized
            LOG4CPLUS_WARN(logger, "Child element not recognized:");
            LOG4CPLUS_WARN(logger, childElement->ValueStr());

            unknownElementFound(childElement);
            return;
        }
    }


    void VisualizationElement::textFound(const char * text)
    {
        LOG4CPLUS_WARN(logger, "Text not recognized:");
        LOG4CPLUS_WARN(logger, text);

        unknownTextFound(text);
    }

    bool VisualizationElement::postConfiguration()
    {
        bool result = false;
        if ( _scene_filename.length() > 0)
        {
            result = true;
        }
        return result && _resolution_processed;
    }


    const std::string& VisualizationElement::getVisualizationAPI() const
    {
        return _visAPI;
    }

    const std::string& VisualizationElement::getSceneFilename() const
    {
        return _scene_filename;
    }

}
